package pong.mathematic;

public class Vector2D extends Vector {
	private float x, y;

	public Vector2D() {
		x = 0;
		y = 0;
	}

	public Vector2D(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public Vector2D(Vector2D vec) {
		x = vec.x;
		y = vec.y;
	}

	@Override
	public void normalize() {
		float length = distance();
		if (length == 0) {
			x = 0;
			y = 0;
		} else {
			x /= length;
			y /= length;
		}
	}

	@Override
	public float distance() {
		return (float) Math.sqrt(x * x + y * y);
	}

	@Override
	public float distance(Vector vec) {
		return (float) Math.sqrt(D(x, ((Vector2D) vec).x) + D(y, ((Vector2D) vec).y));
	}

	public void add(Vector2D vec) {
		x += vec.x;
		y += vec.y;
	}

	public void add(float x, float y) {
		this.x += x;
		this.y += y;
	}

	@Override
	public void add(float decimal) {
		x += decimal;
		y += decimal;
	}

	public void minus(Vector2D vec) {
		x -= vec.x;
		y -= vec.y;
	}

	@Override
	public void minus(float decimal) {
		x -= decimal;
		y -= decimal;
	}

	@Override
	public void multy(float decimal) {
		x *= decimal;
		y *= decimal;
	}

	@Override
	public void divide(float decimal) {
		x /= decimal;
		y /= decimal;
	}

	@Override
	public double angle() {
		return Math.atan2(y, x);
	}

	@Override
	public void rotateCoordinates(double tiltAngle) {
		double angle = angle();
		double mag = distance();
		angle -= tiltAngle;
		x = (float) (mag * Math.cos(angle));
		y = (float) (mag * Math.sin(angle));
	}

	@Override
	public String toString() {
		return "x=" + x + " y=" + y;
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	public void setX(float x) {
		this.x = x;
	}

	public void setY(float y) {
		this.y = y;
	}

	@Override
	public void setVector(Vector vec) {
		if (vec instanceof Vector2D) {
			x = ((Vector2D) vec).x;
			y = ((Vector2D) vec).y;
			return;
		}
		if (vec instanceof Vector3D) {
			x = ((Vector3D) vec).getX();
			y = ((Vector3D) vec).getY();
			return;
		}
	}

	@Override
	public void add(Vector vec) {
		if (vec instanceof Vector2D) {
			x += ((Vector2D) vec).x;
			y += ((Vector2D) vec).y;
			return;
		}
		if (vec instanceof Vector3D) {
			x += ((Vector3D) vec).getX();
			y += ((Vector3D) vec).getY();
			return;
		}
	}

	@Override
	public void minus(Vector vec) {
		if (vec instanceof Vector2D) {
			x -= ((Vector2D) vec).x;
			y -= ((Vector2D) vec).y;
			return;
		}
		if (vec instanceof Vector3D) {
			x -= ((Vector3D) vec).getX();
			y -= ((Vector3D) vec).getY();
			return;
		}
	}
}
